package com.supermap.dialogs;

import com.supermap.consts.AttrConsts;
import com.supermap.data.*;
import com.supermap.data.Enum;
import com.supermap.dialogs.advance.CJDialog;
import com.supermap.mapping.Layer;
import com.supermap.mapping.Layers;
import com.supermap.mapping.Map;
import com.supermap.mapping.Selection;

import javax.swing.*;
import java.awt.*;

public class SpatialQueryDialog extends CJDialog {
    private JComboBox<String> layersComboBox;
    private JComboBox<SpatialQueryMode> spatialQueryModeComboBox;

    private Map map;
    private Recordset selectedRecordset;

    public SpatialQueryDialog(Frame owner, String title, boolean modal, Map map, Recordset recordset) {
        super(owner, title, modal, 300, 120, new Object[]{map, recordset});
    }

    @Override
    protected void initializeArgs(Object[] args) {
        this.map = (Map) args[0];
        this.selectedRecordset = (Recordset) args[1];
    }

    @Override
    protected void initializeCore() {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        super.confirmBtn.setText("查询");

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(2, 1));
        add(mainPanel, BorderLayout.CENTER);

        JLabel queryLayerLabel = new JLabel("查询图层: ");
        layersComboBox = new JComboBox<>();
        Layers layers = map.getLayers();
        for (int i = 0; i < layers.getCount(); i++) {
            Layer layer = layers.get(i);
            if (layer.getDataset() instanceof DatasetVector) {
                layersComboBox.addItem(layer.getName());
            }
        }
        JPanel queryLayerPanel = new JPanel();
        queryLayerPanel.setLayout(new BorderLayout());
        queryLayerPanel.add(queryLayerLabel, BorderLayout.WEST);
        queryLayerPanel.add(layersComboBox, BorderLayout.CENTER);
        mainPanel.add(queryLayerPanel);

        JLabel modeLayerLabel = new JLabel("查询算子: ");
        spatialQueryModeComboBox = new JComboBox<>();
        Enum[] modes = SpatialQueryMode.getEnums(SpatialQueryMode.class);
        for (Enum mode : modes) {
            if (mode == SpatialQueryMode.NONE) {
                continue;
            }
            spatialQueryModeComboBox.addItem((SpatialQueryMode) mode);
        }
        JPanel modePanel = new JPanel();
        modePanel.setLayout(new BorderLayout());
        modePanel.add(modeLayerLabel, BorderLayout.WEST);
        modePanel.add(spatialQueryModeComboBox, BorderLayout.CENTER);
        mainPanel.add(modePanel);
    }

    @Override
    protected void doConfirmCore() {
        SpatialQueryMode mode = (SpatialQueryMode) spatialQueryModeComboBox.getSelectedItem();
        QueryParameter queryParameter = new QueryParameter();
        queryParameter.setSpatialQueryObject(selectedRecordset);
        queryParameter.setSpatialQueryMode(mode);

        String layerName = (String) layersComboBox.getSelectedItem();
        if (layerName == null || layerName.equals(AttrConsts.EMPTY)) {
            return;
        }
        Layer layer = map.getLayers().findLayer(layerName);
        if (layer == null) {
            return;
        }

        DatasetVector dataset = (DatasetVector) layer.getDataset();
        Recordset recordset = dataset.query(queryParameter);
        Selection selection = layer.getSelection();
        selection.setDefaultStyleEnabled(false);
        selection.fromRecordset(recordset);
        GeoStyle selectionStyle = selection.getStyle();
        selectionStyle.setLineColor(Color.RED);
        selectionStyle.setFillForeColor(Color.RED);
        selectionStyle.setFillBackColor(Color.RED);
        selectionStyle.setLineWidth(1);
        selectionStyle.setMarkerSize(new Size2D(6, 6));
        recordset.dispose();
        map.refresh();
    }
}
